package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IMealService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 套餐Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class MealServiceImpl implements IMealService {
    @Resource
    private MealMapper mealMapper;
    @Resource
    private MealDetailMapper mealDetailMapper;
    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询套餐
     *
     * @param id 套餐主键
     * @return 套餐
     */
    @Override
    public Meal selectMealById(Long id) {
        Meal meal = mealMapper.selectMealById(id);
        List<MealDetail> mealDetails = mealDetailMapper.selectMealDetailList(new MealDetail() {{
            setMealId(meal.getId());
        }});
        mealDetails.forEach(item -> {
            if (item.getMenuId() != null) {
                Menu menu = menuMapper.selectMenuById(item.getMenuId());
                if (menu != null) {
                    item.setMenuName(menu.getName());
                }
            }

        });
        meal.setDetails(mealDetails);
        return meal;
    }

    /**
     * 查询套餐列表
     *
     * @param meal 套餐
     * @return 套餐
     */
    @Override
    public List<Meal> selectMealList(Meal meal) {
        return mealMapper.selectMealList(meal);
    }

    /**
     * 查询套餐列表_dose
     */
    @Override
    public List<Meal> selectMealList_dose(Meal meal) {
        List<Meal> meals = mealMapper.selectMealList(meal);
        meals.forEach(item -> {
            List<MealDetail> mealDetails = mealDetailMapper.selectMealDetailList(new MealDetail() {{
                setMealId(item.getId());
            }});
            mealDetails.forEach(detail -> {
                if (detail.getMenuId() != null) {
                    Menu menu = menuMapper.selectMenuById(detail.getMenuId());
                    if (menu != null) {
                        detail.setMenuName(menu.getName());
                    }
                }
            });
            item.setDetails(mealDetails);
        });
        return meals;
    }

    /**
     * 新增套餐
     *
     * @param meal 套餐
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMeal(Meal meal) {
        meal.setCreateTime(DateUtils.getNowDate());

        if (meal.getParams().get("details") == null) {
            throw new ServiceException("未选择菜谱");
        }

        int i = mealMapper.insertMeal(meal);

        insertBatch(meal);

        return i;
    }

    /**
     * 修改套餐
     *
     * @param meal 套餐
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMeal(Meal meal) {
        meal.setUpdateTime(DateUtils.getNowDate());
        if (meal.getParams().get("details") == null) {
            throw new ServiceException("未选择菜谱");
        }
        int i = mealMapper.updateMeal(meal);

        //删除关联
        mealDetailMapper.deleteMealDetailByMealId(meal.getId());

        insertBatch(meal);

        return i;
    }

    /**
     * 批量新增
     */
    private void insertBatch(Meal meal) {
        List<MealDetail> mealDetails = JSON.parseArray(JSON.toJSONString(meal.getParams().get("details")), MealDetail.class);
        mealDetails.forEach(item -> {
            item.setMealId(meal.getId());
        });

        mealDetailMapper.insertMealDetailBatch(new HashMap<String, Object>() {{
            put("details", mealDetails);
        }});
    }

    /**
     * 批量删除套餐
     *
     * @param ids 需要删除的套餐主键
     * @return 结果
     */
    @Override
    public int deleteMealByIds(Long[] ids) {
        int i = mealMapper.deleteMealByIds(ids);
        mealDetailMapper.deleteMealDetailByMealIds(ids);
        return i;
    }

    /**
     * 删除套餐信息
     *
     * @param id 套餐主键
     * @return 结果
     */
    @Override
    public int deleteMealById(Long id) {
        return mealMapper.deleteMealById(id);
    }
}
